<?php
/**
 * @ClassName: WechatLogic
 * @Description: 微信逻辑
 * @author: wyaohu
 * @Date: 2024/9/8 19:49
 * @Version: V1.0.0
 */

namespace app\common\logic;

use EasyWeChat\Factory;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class WechatLogic
{
    /**
     * 公众号
     * @return \EasyWeChat\OfficialAccount\Application
     */
    public static function mp()
    {
        return Factory::officialAccount(config('wechat.mp_config'));
    }

    /**
     * 微信小程序
     * @return \EasyWeChat\MiniProgram\Application
     */
    public static function mini()
    {
        return Factory::miniProgram(config('wechat.mini_config'));
    }

    /**
     * 移动应用
     * @return \EasyWeChat\OpenPlatform\Application
     */
    public static function app()
    {
        return Factory::OpenPlatform(config('wechat.app_config'));
    }

    /**
     * 解析微信小程序 wx.login 登录获得的code
     * @param $code
     * @return mixed|void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    function getMiniSessionCode($code = '', $grant_type = 'authorization_code')
    {
        try {
            $uri = 'https://api.weixin.qq.com/sns/jscode2session?appid=' .
                config('wechat.mini_config.app_id') . '&secret=' . config('wechat.mini_config.secret') .
                '&js_code=' . $code . '&grant_type=' . $grant_type;
            $client = new Client();
            $response = $client->get($uri);
            $data = json_decode($response->getBody()->getContents(), true);
            makelog("jscode2session：" . json_encode($data), 'login/wx', '_mini');

            trace($data);

            if (isset($data['openid']) || isset($data['unionid'])) {
                $data['code'] = 1;
            }
            return $data;
        } catch (GuzzleException $e) {
            trace($e);
        }
        return ['code' => 0, 'msg' => '登录失败'];
    }

    /**
     * 使用授权码获取access_token和openid
     * @param $code
     * @return mixed|void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    function getMpSessionCode($code = '', $grant_type = 'authorization_code')
    {
        try {
            $uri = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' .
                config('wechat.mp_config.app_id') . '&secret=' . config('wechat.mp_config.secret') .
                '&code=' . $code . '&grant_type=' . $grant_type;
            $client = new Client();
            $response = $client->get($uri);
            $data = json_decode($response->getBody()->getContents(), true);
            trace($data);
            if (isset($data['openid'])) {
                $data['code'] = 1;
            }
            return $data;
        } catch (GuzzleException $e) {
            trace($e);
        }
        return ['code' => 0, 'msg' => '登录失败'];
    }

    /**
     * 获取手机号
     * @param $iv
     * @param $encryptedData
     * @param $session_key
     * @return mixed|string
     */
    function getPhone($iv, $encryptedData, $session_key)
    {
        $aesKey = base64_decode($session_key);
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $phoneNumber = json_decode($result, true);
        if ($phoneNumber) {
            return $phoneNumber['purePhoneNumber'];
        } else {
            return "";
        }
    }

}